home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dmake / cmdlist.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  8KB  |  380 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. /*
  8.  *  CMDLIST.c
  9.  *
  10.  */
  11.  
  12. #include "defs.h"
  13.  
  14. Prototype void InitCmdList(void);
  15. Prototype void PutCmdListChar(List *, char);
  16. Prototype void PutCmdListSym(List *, char *, short *);
  17. Prototype void CopyCmdList(List *, List *);
  18. Prototype void AppendCmdList(List *, List *);
  19. Prototype int  PopCmdListSym(List *, char *, long);
  20. Prototype int  PopCmdListChar(List *);
  21. Prototype void CopyCmdListBuf(List *, char *);
  22. Prototype void CopyCmdListNewLineBuf(List *, char *);
  23. Prototype long CmdListSize(List *);
  24. Prototype long CmdListSizeNewLine(List *);
  25. Prototype void CopyCmdListConvert(List *, List *, char *, char *);
  26. Prototype long ExecuteCmdList(DepNode *, List *);
  27.  
  28. List CmdFreeList;
  29. __aligned char CmdTmp1[256];
  30. __aligned char CmdTmp2[256];
  31.  
  32. void
  33. InitCmdList()
  34. {
  35.     NewList(&CmdFreeList);
  36. }
  37.  
  38. void
  39. PutCmdListChar(List *list, char c)
  40. {
  41.     CmdNode *node;
  42.  
  43.     if ((node = GetTail(list)) == NULL || (node->cn_Idx == node->cn_Max)) {
  44.     if ((node = RemHead(&CmdFreeList)) == NULL) {
  45.         node = malloc(sizeof(CmdNode) + 64);
  46.         node->cn_Node.ln_Name = (char *)(node + 1);
  47.         node->cn_Max = 64;
  48.     }
  49.     node->cn_Node.ln_Type = 0;
  50.     node->cn_Idx = 0;
  51.     node->cn_RIndex = 0;
  52.     AddTail(list, &node->cn_Node);
  53.     }
  54.     node->cn_Node.ln_Name[node->cn_Idx++] = c;
  55. }
  56.  
  57. void
  58. PutCmdListSym(list, buf, pspace)
  59. List *list;
  60. char *buf;
  61. short *pspace;
  62. {
  63.     if (*buf) {
  64.     if (pspace) {
  65.         if (*pspace)
  66.         PutCmdListChar(list, ' ');
  67.         *pspace = 1;
  68.     }
  69.     /*if ((node = GetTail(list)) && node->cn_Idx && node->cn_Node.ln_Name[node->cn_Idx-1] != ' ')*/
  70.     while (*buf) {
  71.         PutCmdListChar(list, *buf);
  72.         ++buf;
  73.     }
  74.     }
  75. }
  76.  
  77. void
  78. CopyCmdList(fromList, toList)
  79. List *fromList;
  80. List *toList;
  81. {
  82.     CmdNode *from;
  83.     long n;
  84.     long i;
  85.  
  86.     for (from = GetHead(fromList); from; from = GetSucc(&from->cn_Node)) {
  87.     CmdNode *copy = NULL;
  88.  
  89.     dbprintf(("COPYFROM %*.*s\n", from->cn_Idx, from->cn_Idx, from->cn_Node.ln_Name));
  90.  
  91.     for (n = 0; n < from->cn_Idx; ) {
  92.         if ((copy = RemHead(&CmdFreeList)) == NULL) {
  93.         copy = malloc(sizeof(CmdNode) + 64);
  94.         copy->cn_Max = 64;
  95.         copy->cn_Node.ln_Name = (char *)(copy + 1);
  96.         }
  97.         i = (copy->cn_Max < from->cn_Idx - n) ? copy->cn_Max : from->cn_Idx - n;
  98.         copy->cn_Node.ln_Type = 0;
  99.         copy->cn_Idx = i;
  100.         copy->cn_RIndex = 0;
  101.         movmem(from->cn_Node.ln_Name + n, copy->cn_Node.ln_Name, i);
  102.         AddTail(toList, ©->cn_Node);
  103.         n += i;
  104.     }
  105.     if (copy)
  106.         copy->cn_Node.ln_Type = from->cn_Node.ln_Type;
  107.     }
  108. }
  109.  
  110. void
  111. AppendCmdList(fromList, toList)
  112. List *fromList;
  113. List *toList;
  114. {
  115.     CmdNode *from;
  116.  
  117.     while ((from = RemHead(fromList)) != NULL)
  118.     AddTail(toList, &from->cn_Node);
  119. }
  120.  
  121.  
  122. /*
  123.  *  pop a symbol (symbols are separated by white space)
  124.  */
  125.  
  126. int
  127. PopCmdListSym(cmdList, buf, max)
  128. List *cmdList;
  129. char *buf;
  130. long max;
  131. {
  132.     short c;
  133.     short i = 0;
  134.  
  135.     --max;
  136.  
  137.     while ((c = PopCmdListChar(cmdList)) == ' ' || c == '\t' || c == '\n')
  138.     ;
  139.     while (c != EOF && c != ' ' && c != '\t' && c != '\n' && i < max) {
  140.     buf[i++] = c;
  141.     c = PopCmdListChar(cmdList);
  142.     }
  143.     buf[i] = 0;
  144.     return((i) ? 0 : -1);
  145. }
  146.  
  147. int
  148. PopCmdListChar(cmdList)
  149. List *cmdList;
  150. {
  151.     CmdNode *node;
  152.     short c = EOF;
  153.  
  154.     while ((node = GetHead(cmdList)) != NULL) {
  155.     if (node->cn_RIndex != node->cn_Idx)
  156.         return((ubyte)node->cn_Node.ln_Name[node->cn_RIndex++]);
  157.     Remove((struct Node *)node);
  158.     AddTail(&CmdFreeList, &node->cn_Node);
  159.     }
  160.     return(c);
  161. }
  162.  
  163. void
  164. CopyCmdListBuf(list, buf)
  165. List *list;
  166. char *buf;
  167. {
  168.     CmdNode *node;
  169.  
  170.     while ((node = RemHead(list)) != NULL) {
  171.     movmem(node->cn_Node.ln_Name + node->cn_RIndex, buf, node->cn_Idx - node->cn_RIndex);
  172.     buf += node->cn_Idx;
  173.     AddTail(&CmdFreeList, &node->cn_Node);
  174.     }
  175.     buf[0] = 0;
  176. }
  177.  
  178. void
  179. CopyCmdListNewLineBuf(list, buf)
  180. List *list;
  181. char *buf;
  182. {
  183.     CmdNode *node;
  184.     long i;
  185.  
  186.     while ((node = RemHead(list)) != NULL) {
  187.     for (i = node->cn_RIndex; i < node->cn_Idx && node->cn_Node.ln_Name[i] != '\n'; ++i)
  188.         *buf++ = node->cn_Node.ln_Name[i];
  189.     if (i != node->cn_Idx) {
  190.         node->cn_RIndex = i + 1;
  191.         AddHead(list, &node->cn_Node);
  192.         break;
  193.     }
  194.     AddTail(&CmdFreeList, &node->cn_Node);
  195.     }
  196.     *buf = 0;
  197. }
  198.  
  199. long
  200. CmdListSize(list)
  201. List *list;
  202. {
  203.     CmdNode *node;
  204.     long n = 0;
  205.  
  206.     for (node = GetHead(list); node; node = GetSucc(&node->cn_Node))
  207.     n += node->cn_Idx - node->cn_RIndex;
  208.     return(n);
  209. }
  210.  
  211. long
  212. CmdListSizeNewLine(list)
  213. List *list;
  214. {
  215.     CmdNode *node;
  216.     long n = 0;
  217.     long i;
  218.  
  219.     for (node = GetHead(list); node; node = GetSucc(&node->cn_Node)) {
  220.     for (i = node->cn_RIndex; i < node->cn_Idx && node->cn_Node.ln_Name[i] != '\n'; ++i)
  221.         ;
  222.     n += i - node->cn_RIndex;
  223.     if (i != node->cn_Idx)
  224.         break;
  225.     }
  226.     return(n);
  227. }
  228.  
  229. void
  230. CopyCmdListConvert(fromList, toList, srcMat, dstMat)
  231. List *fromList;
  232. List *toList;
  233. char *srcMat;
  234. char *dstMat;
  235. {
  236.     List tmpList;
  237.     short space = 0;
  238.  
  239.     dbprintf(("fromlist %08lx copyconvert '%s' -> '%s'\n", GetHead(fromList), srcMat, dstMat));
  240.     srcMat = ExpandVariable(srcMat, NULL);
  241.     dstMat = ExpandVariable(dstMat, NULL);
  242.  
  243.     NewList(&tmpList);
  244.     CopyCmdList(fromList, &tmpList);
  245.  
  246.     if (!*srcMat)
  247.     {
  248.        if (!GetHead(&tmpList))
  249.        {
  250.           PutCmdListSym(toList, dstMat, &space);
  251.           return;
  252.        }
  253.        /* We need to replace the src and destination with meaningful wildcards */
  254.        srcMat = dstMat = "*";
  255.     }
  256.  
  257.     /*
  258.      *    run each symbol through the conversion
  259.      */
  260.  
  261.     while (PopCmdListSym(&tmpList, CmdTmp1, sizeof(CmdTmp1)) == 0)
  262.     {
  263.     if (WildConvert(CmdTmp1, CmdTmp2, srcMat, dstMat) == 0)
  264.     {
  265.         PutCmdListSym(toList, CmdTmp2, &space);
  266.     }
  267.     }
  268. }
  269.  
  270. /*
  271.  *  The command list is executed by making a duplicate of it then reparsing
  272.  *  it resolving variable references
  273.  *
  274.  */
  275.  
  276. long
  277. ExecuteCmdList(dep, list)
  278. DepNode *dep;
  279. List *list;
  280. {
  281.     List tmpSrc;
  282.     List tmpDst;
  283.     short c;
  284.     long r = 0;
  285.  
  286.     NewList(&tmpSrc);
  287.     NewList(&tmpDst);
  288.     CopyCmdList(list, &tmpSrc);
  289.  
  290.     while ((c = PopCmdListChar(&tmpSrc)) != EOF) {
  291.     if (c == '$' || c == '%') {
  292.         short c0 = c;
  293.  
  294.         c = PopCmdListChar(&tmpSrc);
  295.         if (c == '(') {     /*  Variable Ref */
  296.         char *spec = AllocPathBuffer();
  297.  
  298.         /*
  299.          *  copy variable specification into a buffer then resolve
  300.          *  it to tmpDst
  301.          */
  302.         spec[0] = c0;
  303.         spec[1] = c;
  304.         for (c0 = 2; (c = PopCmdListChar(&tmpSrc)) != EOF && c != ')' && c0 < PBUFSIZE - 3; ++c0) {
  305.             if (c == '\"') {
  306.             spec[c0++] = c;
  307.             while ((c = PopCmdListChar(&tmpSrc)) != EOF && c != '\"' && c0 < PBUFSIZE - 3)
  308.                 spec[c0++] = c;
  309.             }
  310.             spec[c0] = c;
  311.         }
  312.         if (c != ')')
  313.             error(FATAL, "bad variable spec in command list for %s", dep->dn_Node.ln_Name);
  314.         spec[c0++] = c;
  315.         spec[c0] = 0;
  316.         ExpandVariable(spec, &tmpDst);
  317.         FreePathBuffer(spec);
  318.         continue;
  319.         }
  320.         if (c != c0)        /*  $$, %% escape   */
  321.         PutCmdListChar(&tmpDst, c0);
  322.     }
  323.     PutCmdListChar(&tmpDst, c);
  324.     }
  325.  
  326.     /*
  327.      *    pop into a command buffer for execution
  328.      */
  329.  
  330.     {
  331.     long n;
  332.  
  333.     while (r <= EXIT_CONTINUE && (n = CmdListSizeNewLine(&tmpDst))) {
  334.         short allocated;
  335.         short quiet = 0;
  336.         short ignore= 0;
  337.         char *cmd;
  338.  
  339.         if (n >= sizeof(CmdTmp1) - 2) {    /*  avoid malloc    */
  340.         allocated = 1;
  341.         cmd = (char *)malloc(n + 2);
  342.         } else {
  343.         allocated = 0;
  344.         cmd = CmdTmp1;
  345.         }
  346.         while ((c = PopCmdListChar(&tmpDst)) != EOF && (c == ' ' || c == '\t'))
  347.         --n;
  348.         if (c == '@') {
  349.         quiet = 1;
  350.         c = PopCmdListChar(&tmpDst);
  351.         --n;
  352.         }
  353.         if (c == '-') {
  354.         ignore = 1;
  355.         c = PopCmdListChar(&tmpDst);
  356.         --n;
  357.         }
  358.         cmd[0] = c;
  359.         CopyCmdListNewLineBuf(&tmpDst, cmd + 1);
  360.  
  361.         if (c) {
  362.         cmd[n] = 0;
  363.  
  364.         if (quiet == 0)
  365.             printf("    %s\n", cmd);
  366.         if (NoRunOpt == 0 && cmd[0] != '#') {
  367.             r = Execute_Command(cmd, ignore);
  368.             if (r < 0)
  369.             r = 20;
  370.             if (ExitCode < r)
  371.             ExitCode = r;
  372.         }
  373.         }
  374.         if (allocated)
  375.         free(cmd);
  376.     }
  377.     }
  378.     return(r);
  379. }
  380.